﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Microsoft.SharePoint;

namespace Sapiens.at.SharePoint.Mail
{
    /// <summary>
    /// Base class with standard implementation for a mail message
    /// </summary>
    public abstract class MailMessageBase
    {
        /// <summary>
        /// SPWeb
        /// </summary>
        public SPWeb Web;
        /// <summary>
        /// MailMessage
        /// </summary>
        public MailMessage Message = new MailMessage();
        
        /// <summary>
        /// Instantiates the MailMessageBase
        /// </summary>
        /// <param name="web">SPWeb</param>
        public MailMessageBase(SPWeb web)
        {
            Web = web;
        }

        #region Properties
        private SmtpClient _client;
        /// <summary>
        /// Gets or sets the SmtpClient.
        /// Default: SMTP Server, that is configured in the outbound mail settings of the current webapplication
        /// </summary>
        public SmtpClient Client
        {
            get
            {
                if (_client == null)
                {
                    _client = new SmtpClient(Web.Site.WebApplication.OutboundMailServiceInstance.Server.Address, 25);
                }
                return _client;
            }
            set { _client = value; }
        }

        private string _defaultMailDomain;
        /// <summary>
        /// Gets or sets the DefaultMailDomain.
        /// Default: Default EMail Address, which is configured in the mail settings of the farm
        /// </summary>
        public string DefaultMailDomain
        {
            get
            {
                if (_defaultMailDomain == null)
                    _defaultMailDomain = ((Microsoft.SharePoint.Administration.SPIncomingEmailService)Web.Site.WebApplication.Farm.GetChild<Microsoft.SharePoint.Administration.SPIncomingEmailService>()).ServerDisplayAddress;
                return _defaultMailDomain;
            }
            set
            {
                _defaultMailDomain = value;
            }
        }
        #endregion Properties

        #region Methods
        /// <summary>
        /// Sends the mailmessage.
        /// Defines the List as the Sender of the email.
        /// </summary>
        /// <param name="List">EMail enabled SPList</param>
        public virtual void Send(SPList List)
        {
            if (string.IsNullOrEmpty(List.EmailAlias)) { throw new SPException(Resources.Sapiens_SharePoint.ErrorNoMailOnList); }
            Message.From = new MailAddress(List.EmailAlias + "@" + DefaultMailDomain);
            Send();
        }

        /// <summary>
        /// Sends the mailmessage
        /// </summary>
        public virtual void Send()
        {
            if (Message.From == null) Message.From = new MailAddress(Web.Site.WebApplication.OutboundMailSenderAddress);
            Message.IsBodyHtml = true;
            Client.Send(Message);
        }
        #endregion Methods
    }
}
